package com.iss.soc.saibong.manager;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.iss.soc.exception.SocServiceException;
import com.iss.soc.saibong.bean.*;
import okhttp3.ResponseBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ServiceCall extends ManagerBase {
    private final Logger logger = LoggerFactory.getLogger(ServiceCall.class);
    /**
     * 服务执行REST调用.
     */
    private Service service;
    /**
     * 服务客户机类。
     */
    private ManagerBase client;

    /**
     * 初始化实例
     *
     * @param retrofit
     * @param client
     */
    public ServiceCall(Retrofit retrofit, ManagerBase client) {
        this.service = retrofit.create(Service.class);
        this.client = client;
    }

    public ServiceCall(Retrofit retrofit) {
        this.service = retrofit.create(Service.class);
    }

    /**
     * 该接口定义的所有服务使用
     * 使用改造执行实际上REST调用。该接口定义的所有服务使用
     */
    interface Service {
        //新增
        @Headers("Content-Type: application/json; charset=utf-8")
        @POST("{apiVersion}/register/")
        Call<ResponseBody> register(@Path("apiVersion") String apiVersion, @Body Role rules);

        //修改
        @Headers("Content-Type: application/json; charset=utf-8")
        @PUT("{apiVersion}/updateService/{code}")
        Call<ResponseBody> updateService(@Path("apiVersion") String apiVersion, @Path("id") String code, @Body Role rules);

        /**
         * 单条件查询
         */
        @Headers("Content-Type: application/json; charset=utf-8")
        @GET("{apiVersion}/applyManager/applyManagerPage")
        Call<ResponseBody> searchForCode(@Path("apiVersion") String apiVersion, @Query("id") String id);

        //查询服务
        @Headers("Content-Type: application/json; charset=utf-8")
        @POST("{apiVersion}/saibongs/")
        Call<ResponseBody> searchList(@Path("apiVersion") String apiVersion, @Body Role sa);
    }

    /**
     * @param rules
     * @return
     * @throws IOException
     * @throws IllegalArgumentException
     */
    public Role searchForCode(Role rules) throws SocServiceException, InterruptedException {
        Call<ResponseBody> call = service.searchForCode(client.apiVersion, rules.getId());
        Response<ResponseBody> result = null;
        ResponseBody responseBody = null;
        Role body = null;
        try {
            result = call.execute();
            responseBody = client.restClient.getPostOrDeleteResult(result);
            Gson gson = new Gson();
            body = gson.fromJson(responseBody.string(), new TypeToken<Role>() {
            }.getType());
        } catch (IOException e) {
            logger.error("网络异常，调用服务失败");
            throw new SocServiceException("网络异常，调用服务失败");
        }
        return body;

    }

    /**
     * @param rules
     * @throws IOException
     * @throws IllegalArgumentException
     */
    public String updateService(Role rules) throws SocServiceException, InterruptedException {
        Call<ResponseBody> call = service.updateService(this.client.apiVersion, rules.getId(), rules);
        Response<ResponseBody> result = null;
        ResponseBody responseBody = null;
        String body = "";
        try {
            result = call.execute();
            responseBody = client.restClient.getPostOrDeleteResult(result);
            body = responseBody.string();
        } catch (IOException e) {
            logger.error("网络异常，调用服务失败");
            throw new SocServiceException("网络异常，调用服务失败");
        }
        return body;

    }

    /**
     * @param rules
     * @return
     * @throws IOException
     * @throws IllegalArgumentException
     */
    public String register(Role rules) throws SocServiceException, InterruptedException {
        Call<ResponseBody> call = service.register(this.client.apiVersion, rules);
        Response<ResponseBody> result = null;
        ResponseBody responseBody = null;
        String body = "";
        try {
            result = call.execute();
            responseBody = client.restClient.getPostOrDeleteResult(result);
            body = responseBody.string();
        } catch (IOException e) {
            logger.error("网络异常，调用服务失败");
            throw new SocServiceException("网络异常，调用服务失败");
        }
        return body;

    }

    /**
     * @return
     * @throws IOException
     * @throws IllegalArgumentException
     */
    public List<Role> searchList(Role role) throws SocServiceException, InterruptedException {
        Call<ResponseBody> call = service.searchList(this.client.apiVersion, role);
        Response<ResponseBody> result = null;
        ResponseBody responseBody = null;
        List<Role> body = new ArrayList<>();
        try {
            result = call.execute();
            responseBody = client.restClient.getPostOrDeleteResult(result);
            Gson gson = new Gson();
            body = gson.fromJson(responseBody.string(), new TypeToken<List<Role>>() {
            }.getType());
        } catch (IOException e) {
            logger.error("网络异常，调用服务失败");
            throw new SocServiceException("网络异常，调用服务失败");
        }
        return body;


    }


}
